package Gui;


import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import DB.Importer;
import DB.PhraseConnector;

import com.swtdesigner.FocusTraversalOnArray;

public class App {

	static registerFrame regF;
	static addPlayer gf;
	static PlayerNamesFrame webgf;
	static AddCategoryFrame categoryFrm;
	private JFrame frmWheelOfFortune;
	private BackgroundPanel background;
	static RoatatingImagePanel panel;
	private static IGame game;
	static ArrayList<Player> playersList;
	private ImportWorker worker = null;

	public static IGame getGame() {
		return game;
	}

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		webgf = new PlayerNamesFrame();
		categoryFrm = new AddCategoryFrame();
		playersList = new ArrayList<Player>();
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					App window = new App();
					window.frmWheelOfFortune.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public static boolean isPlaying(String name){
		for (Player p : playersList){
			if (p.getName().equals(name)){
				return true;
			}
		}
		return false;
	}

	public static boolean addPlayer(String name){
		if (isPlaying(name)) {
			return false;
		}
		Player newP = new Player (name);
		return playersList.add(newP);
	}

	public static void clearPlayerList(){
		playersList = new ArrayList<Player>();
	}

	public static int getNumOfPlayers(){
		return playersList.size();
	}

	public static Player getPlayer(int index){
		return playersList.get(index);
	}

	/**
	 * Create the application.
	 */
	public App() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		Initiallizer init = new Initiallizer();
		init.start();
		frmWheelOfFortune = new JFrame();
		frmWheelOfFortune.setResizable(false);
		frmWheelOfFortune.setForeground(new Color(0, 204, 0));
		frmWheelOfFortune.getContentPane().setBackground(new Color(51, 153, 255));
		frmWheelOfFortune.setTitle("WHEEL OF FORTUNE - MAIN MENU");
		frmWheelOfFortune.setBounds(100, 100, 610, 320);
		frmWheelOfFortune.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		background=new BackgroundPanel("/wheelLogo.jpg",false);
		background.setSize(this.frmWheelOfFortune.getWidth(),this.frmWheelOfFortune.getHeight());
		worker = new ImportWorker(); 
		GroupLayout groupLayout = new GroupLayout(frmWheelOfFortune.getContentPane());
		groupLayout.setHorizontalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
						.addComponent(background, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addContainerGap(22, Short.MAX_VALUE))
		);
		groupLayout.setVerticalGroup(
				groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(background, GroupLayout.PREFERRED_SIZE, 295, Short.MAX_VALUE)
		);
		GridBagLayout gbl_background = new GridBagLayout();
		gbl_background.columnWidths = new int[]{101, 83, 53, 85, 93, 85, 53, 0};
		gbl_background.rowHeights = new int[]{23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_background.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_background.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		background.setLayout(gbl_background);

		JButton btnNewGame = new JButton("New Game");
		btnNewGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				clearPlayerList();
				App.webgf.clearUserTable();
			}
		});
		GridBagConstraints gbc_button_1 = new GridBagConstraints();
		gbc_button_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_button_1.anchor = GridBagConstraints.NORTH;
		gbc_button_1.insets = new Insets(0, 0, 5, 5);
		gbc_button_1.gridx = 0;
		gbc_button_1.gridy = 9;
		background.add(btnNewGame, gbc_button_1);
		btnNewGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				gf=new addPlayer();
				gf.setVisible(true);
			}
		});
		JButton btnScores;
		JButton btnTopScorers;
		
		JButton btnUpdateDb = new JButton("Update DB");
		btnUpdateDb.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (worker.isAlive()){
					JOptionPane.showMessageDialog(null, "Import is already running");
				}
				else{
					JOptionPane.showMessageDialog(null, "update will start immidiately. press OK to continue");
					worker.start();
				}
			}
		});
		
		btnTopScorers = new JButton("Top Scorers");
		btnTopScorers.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				topScorersFrame hf=new topScorersFrame();
				hf.setVisible(true);
			}
		});
		GridBagConstraints gbc_button_5 = new GridBagConstraints();
		gbc_button_5.fill = GridBagConstraints.HORIZONTAL;
		gbc_button_5.anchor = GridBagConstraints.NORTH;
		gbc_button_5.insets = new Insets(0, 0, 5, 0);
		gbc_button_5.gridx = 6;
		gbc_button_5.gridy = 10;
		background.add(btnTopScorers, gbc_button_5);
		GridBagConstraints gbc_btnUpdateDb = new GridBagConstraints();
		gbc_btnUpdateDb.fill = GridBagConstraints.HORIZONTAL;
		gbc_btnUpdateDb.insets = new Insets(0, 0, 5, 5);
		gbc_btnUpdateDb.gridx = 0;
		gbc_btnUpdateDb.gridy = 11;
		background.add(btnUpdateDb, gbc_btnUpdateDb);
		
		btnScores = new JButton("Top Scores");
		btnScores.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				highScoresFrame hf=new highScoresFrame();
				hf.setVisible(true);
			}
		});
		GridBagConstraints gbc_button_4 = new GridBagConstraints();
		gbc_button_4.fill = GridBagConstraints.HORIZONTAL;
		gbc_button_4.anchor = GridBagConstraints.NORTH;
		gbc_button_4.insets = new Insets(0, 0, 5, 0);
		gbc_button_4.gridx = 6;
		gbc_button_4.gridy = 9;
		background.add(btnScores, gbc_button_4);
		
		JButton AddCategoryBtn = new JButton("Add Category");
		AddCategoryBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				clearPlayerList();
				App.categoryFrm.clearFrame();
				App.categoryFrm.setVisible(true);
			}
		});
		
				GridBagConstraints gbc_AddCategoryBtn = new GridBagConstraints();
				gbc_AddCategoryBtn.insets = new Insets(0, 0, 5, 5);
				gbc_AddCategoryBtn.gridx = 0;
				gbc_AddCategoryBtn.gridy = 10;
				background.add(AddCategoryBtn, gbc_AddCategoryBtn);
						
								JButton btnQuit = new JButton("Quit");
								GridBagConstraints gbc_btnQuit = new GridBagConstraints();
								gbc_btnQuit.fill = GridBagConstraints.HORIZONTAL;
								gbc_btnQuit.insets = new Insets(0, 0, 5, 0);
								gbc_btnQuit.anchor = GridBagConstraints.NORTH;
								gbc_btnQuit.gridx = 6;
								gbc_btnQuit.gridy = 11;
								background.add(btnQuit, gbc_btnQuit);
								btnQuit.setForeground(Color.BLACK);
		btnQuit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		frmWheelOfFortune.getContentPane().setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{btnQuit, btnNewGame, btnScores}));

		frmWheelOfFortune.getContentPane().setLayout(groupLayout);
		initDataBindings();
	}

	public static void readytoplayREG() {
		if (regF!= null)regF.setVisible(false);
	}


	public static void readytoplay(Player[] players, int numofplayers) {
		if (gf!= null)gf.setVisible(false);

		game = new OfflineGame(players,numofplayers);
		game.setVisible(true);
		game.start();
	}


	protected void initDataBindings() {
	}
	
	public class ImportWorker extends Thread {
	    public void run() {
	    	Importer importer = new Importer();
			String suc  = importer.importDatabases();
			JOptionPane.showMessageDialog(null, suc);
	    }
	}
	public class Initiallizer extends Thread {
	    public void run() {
	    	boolean isFilled = PhraseConnector.getInstance().InitializeCache();
	    	if (!isFilled){
	    		JOptionPane.showMessageDialog(null, "DB seems to be empty, or out of reach.\n" +
	    				"Please check configuration and connection to DB, or try update the DB.");
	    	}
			
	    }
	}
}
